import {Component, Input, OnInit} from '@angular/core';
import {SysUser, SysUserCondition} from '../../sys-user.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifySysUserComponent} from '../modify-sys-user.component';
import {SysOrganizeCondition} from "../../../sys-organize/sys-organize.type";
import {Md5} from "ts-md5";
import {SysRoleCondition} from "../../../sys-role/sys-role.type";

@Component({
    selector: 'app-main-modify-sys-user',
    templateUrl: './main-modify-sys-user.component.html',
    styleUrls: ['./main-modify-sys-user.component.css']
})
export class MainModifySysUserComponent implements OnInit {

    errRes = new ErrRes();

    domain: SysUser = new SysUser();

    extend = {
        domain: {},
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: SysUserCondition = new SysUserCondition();
            condition.uuid = uuid;
            Services.sysUser.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                }
            });
        },
        update: {
            req: (event) => {
                let assign = Object.assign({}, this.domain);
                assign.serPass =assign.serPass && Md5.hashStr(assign.serPass).toString();
                Services.sysUser.update(assign, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.sysUser.list, {
                        moduleId: "system-management",
                        menuId: "sys-user"
                    }]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    organizeDomains = [];
    roleDomains = [];
    initOrganizeAndRoleDomains = () => {
        const sysOrganize = new SysOrganizeCondition();
        Services.sysOrganize.list(sysOrganize, {
            page: 0,
            size: 200,
            orders: "createTime-desc,serNum-asc"
        }).subscribe(item => {
            this.organizeDomains = item.page.content;
        });

        const sysRole = new SysRoleCondition();
        Services.sysRole.list(sysRole, {
            page: 0,
            size: 200,
            orders: "createTime-desc,serNum-asc"
        }).subscribe(item => {
            this.roleDomains = item.page.content;
        });
    };

    constructor(public modifySysUser: ModifySysUserComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.initOrganizeAndRoleDomains();
        this.modifySysUser.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
    }

}
